


var SERVER_PORT = "http://120.77.223.88:8080/red/";

var storePage = new Vue({
    el: "#app-sp",
    data: {
        //登录账户
        isLogin:false,
        phone: "",
        nickname:"",

        //当前输入框输入内容
        searchContent: "",

        //一级目录
        currentPrimaryCategory: [],
        //二级目录
        currentSecondCatrgory: [],
        //目录树
        categoryTree: [],

        //当前一级目录下的所有商品
        currentGoodsInPrimCate: [],

        //当前二级目录下的所有商品
        currentGoodsInSubCate: [],

        //当前需要显示的商品
        currentGoodsToDisplay: [],


        
    },

    methods: {
        //退出登录
        quitLogin: function () {
            this.isLogin = false;
            this.phone = "";
            this.nickname = "";
            window.localStorage.setItem("isLogin", false);
            window.location.reload(true);
        },

        onPrimCateClick: function (index) {
            var ID = this.categoryTree[index].primaryCategory.ID;
            this.requestGoodsByPrimCateId(ID);
        },

        onSideBarSubCateClick: function (index, sub_index) {

            console.log(index + " " + sub_index);

            var ID = this.categoryTree[index].secondCategory[sub_index].ID;
           

            if (ID == -1) {
                this.requestGoodsByPrimCateId(this.categoryTree[index].primaryCategory.ID);
            } else {
                this.requestGoodsSubCateId(ID);
            }

        },

        onGoodItemClick:function(index){
            var ID = this.currentGoodsToDisplay[index].goods_ID;
            //window.localStorage.setItem("good_id",ID);
            window.localStorage.setItem("good_id",ID);
            window.location.href="pro-detail.html"
        },

        //得到所有一级目录
        getAllCategory: function () {
            this.$http.get(SERVER_PORT + "requestCategory").then(
                response => {
                    //console.log(response.data.data);
                    var originalChaoticInfo = response.data.data;

                    for (var index in originalChaoticInfo) {
                        var primaryCategoryName = originalChaoticInfo[index].category1_Name;
                        var primaryCategoryID = originalChaoticInfo[index].category1_ID;

                        var primaryCategory = {
                            name: primaryCategoryName,
                            ID: primaryCategoryID
                        }
                        var secondCategory = [{
                            name: "该类全部",
                            ID: -1,
                        }];

                        for (var sub_index in originalChaoticInfo[index].category2) {
                            var secondCategoryName =
                                originalChaoticInfo[index].category2[sub_index].name;
                            var category2_ID =
                                originalChaoticInfo[index].category2[sub_index].category2_ID;
                            secondCategory.push({
                                name: secondCategoryName,
                                ID: category2_ID
                            });
                        }

                        this.categoryTree.push({
                            primaryCategory: primaryCategory,
                            secondCategory: secondCategory
                        });
                    }

                    var subID = window.localStorage.getItem("subID");
                    var primIndex = window.localStorage.getItem("primIndex_goodCart");
                    var searchContent = window.localStorage.getItem("searchContent");
                    console.log(primIndex);
                    if(subID!=undefined)
                    {
                        console.log("Jump "+subID);
                        this.requestGoodsSubCateId(subID);
                        window.localStorage.removeItem("subID");
                    }
                    else if(primIndex!=undefined)
                    {
                        var primID = this.categoryTree[primIndex].primaryCategory.ID;
                        window.localStorage.removeItem("primIndex_goodCart");
                        this.requestGoodsByPrimCateId(primID);
                    }
                    else if(searchContent!=undefined){
                        this.searchContent=searchContent;
                        window.localStorage.removeItem("searchContent");
                        this.search();
                    }
                    else
                        this.requestGoodsByPrimCateId(this.categoryTree[0].primaryCategory.ID);

                    //this.sellers = Object.assign([], response.data.data);
                    // 响应成功回调
                },
                response => {
                    // 响应错误回调
                    alert("请求失败");
                }
            );
        },

        //根据一级目录ID请求商品
        requestGoodsByPrimCateId: function (ID) {
            //console.log(ID);
            this.$http.post(SERVER_PORT + "requestGoodsInCategory1", { category1_ID: ID }, {
                emulateJSON: true
            }).then((response) => {
                this.currentGoodsInPrimCate = response.data.data;
                this.currentGoodsToDisplay = response.data.data;
            }, (error) => {
                alert("请求失败");
            })
        },

        //根据二级目录ID请求商品
        requestGoodsSubCateId: function (ID) {

            console.log(ID);
            this.$http.post(SERVER_PORT + "requestGoodsInCategory2", { category2_ID: ID }, {
                emulateJSON: true
            }).then((response) => {
                this.currentGoodsInSubCate = response.data.data;
                this.currentGoodsToDisplay = response.data.data;
            }, (error) => {
                alert("请求失败");
            })
        },

        //展开下拉栏
        popupSidebar: function (index) {
            var index;
            var num;
            var height;
            var ulheight;
            var t;

            index = index + 1;
            //index = $(this).parent().index();
            //              得到该li下面的ul儿子
            t = $(".product-list-nav").eq(index).children("ul");

            //              得到ul高度判断点击后ul是显示还是隐藏
            ulheight = t.css("height");
            //              显示和隐藏时的动画
            t.css("transition", "height .5s");
            //              隐藏
            if (ulheight != "1px") {
                //                  高度值变为1
                $(".product-list-nav").eq(index).children("ul").css("height", "1px");
                t.siblings("h3").children("span").css("transform", "rotate(0deg");
            }
            //              显示
            else {
                //                  得到该ul下面有多少个li，然后乘以每个li的offsetHeight（height+padding+border值）值，将这个值赋给ul
                num = t.children("li").length;
                height = t.children("li")[1].offsetHeight;
                t.css("height", (num * height).toString() + "px");
                t.siblings("h3").children("span").css("transform", "rotate(90deg");
            }

        },

        //搜索
        search: function () {

            if(this.searchContent.length==0)
            {
                Ply.dialog("alert", "输入不能为空").done(function () {
                });
                return;
            }
            
            console.log(this.searchContent)
            this.$http.post(SERVER_PORT + "searchGoods", { search_str: this.searchContent }, {emulateJSON: true})
            .then(response => {
                console.log(response.data.data)
                var goods_searched = response.data.data;
                if (goods_searched.length == 0) {
                    Ply.dialog("alert", "无任何结果").done(function () {
                    });
                }
                else
                {
                    this.currentGoodsToDisplay = goods_searched;
                }

            }, error => {
                alert("查询失败")
            })
        },

    },

    created() {
        if (window.localStorage.getItem("isLogin") !== undefined && window.localStorage.getItem("isLogin") !== "false") {
            this.isLogin = true;
            this.nickname = window.localStorage.getItem("nickname");
            this.phone = window.localStorage.getItem('phone');
        }
        this.getAllCategory();
    },

});
